<template>


  <el-dialog
    top="20px"
    :title="defaultValue.title"
    :lock-scroll="tableConfig.lockScroll"
    :append-to-body="tableConfig.appendToBody"
    :visible="tableConfig.show"
    @close="tableConfig.show = false"
    :show-close="tableConfig.close"
    :destroy-on-close="tableConfig.destroyOnClose"
    width="1100px">

    <el-form size="small" ref="formOpsComputerFileRef" :rules="checkRule" :model="submitData" label-width="180px">

        <el-form-item label="名称" prop="name">
            <el-input v-model="submitData.name" />
        </el-form-item>
        <el-form-item label="类型" prop="fileOrDir">
          <el-select v-model="submitData.fileOrDir" placeholder="请选择" clearable>
            <el-option :key="1" label="文件" :value="1" />
            <el-option :key="2" label="目录" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="路径" prop="path">
            <el-input v-model="submitData.path" />
        </el-form-item>
        <el-form-item label="重要性" prop="core">
          <el-select v-model="submitData.core" placeholder="请选择" clearable>
            <el-option :key="1" label="高" :value="1" />
            <el-option :key="2" label="中" :value="2" />
            <el-option :key="3" label="低" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item label="详情说明" prop="description">
          <el-input v-model.trim="submitData.description" type="textarea" :autosize="{minRows: 3}" />
        </el-form-item>

    </el-form>


    <span slot="footer" class="dialog-footer">
      <el-button plain type="warning" @click="toSaveUpd"
                 v-if="defaultValue.saveUpdSelect !== 'select'">{{ defaultValue.title }}
                 </el-button>
      <el-button plain type="primary" @click="tableConfig.show = false">关闭</el-button>
    </span>

  </el-dialog>


</template>

<script>

import http from '@/utils/http'
import {clearInvalid} from "@/utils/utils"

export default {
  name: "OpsComputerFileSaveUpd",
  data() {
    return {
      // 表格的默认值
      tableConfig: {
        // 关闭时销毁 Dialog 中的元素
        destroyOnClose: true,
        // 弹出框是否打开
        show: false,
        // 是否显示关闭按钮
        close: true,
        // 下面2个属性解决打开弹出框后没有在最上面显示的问题，导致看到的内容是一半的，需要先将滚动条移到最上面
        lockScroll: false,
        appendToBody: true,
      },
      // 默认值，进入页面后的值
      defaultValue: {
        // 弹出框标题
        title: '查看',
        // 保存还是修改or查看,取值有 select | save | upd
        saveUpdSelect: 'select'
      },
      // 提交的数据
      submitData:{
        id: null,
        siteId: null,
        computerServerId: null,
        fileOrDir: null,
        name: null,
        path: null,
        core: null,
        description: null,
      },
      // 校验规则
      checkRule: {
        fileOrDir: [{required: true, message: '请输入文件还是目录', trigger: 'change'}],
        name: [{required: true, message: '请输入名称', trigger: 'blur'}],
        path: [{required: true, message: '请输入路径', trigger: 'blur'}],
        core: [{required: true, message: '请输入重要性', trigger: 'change'}],
      },
    }
  },
  methods: {
    /**
     * 初始化操作
     * @param operateType 操作类型,保存还是修改or查看,取值有 select \ save \ upd
     * @param obj 如果是修改和查看，则是详细数据,需要赋值
     * @param computerServerId 所属计算机，只有新增的时候需要
     */
    init(computerServerId, operateType, obj){
      this.defaultValue.saveUpdSelect = operateType
      this.clearOldData()

      if (operateType === 'save'){
        this.defaultValue.title = '新增'
        this.submitData.computerServerId = computerServerId

      } else if (operateType === 'upd'){
        this.defaultValue.title = '修改'
        // 要json处理下，否则会影响之前的值
        this.submitData = JSON.parse(JSON.stringify(obj))

      } else {
        // 可能查看
      }

      this.tableConfig.show = true
    },
    /**
     * 清理掉旧数据
     */
    clearOldData(){
      this.submitData = {
        id: null,
        siteId: null,
        computerServerId: null,
        fileOrDir: 2,
        name: null,
        path: null,
        core: 3,
        description: null,
      }
    },
    /**
     * 是保存还是修改
     */
    toSaveUpd(){
      if (this.defaultValue.saveUpdSelect === 'save'){
        this.saveOpsComputerFile()

      } else if (this.defaultValue.saveUpdSelect === 'upd'){
        this.updOpsComputerFile()

      } else {

      }
    },
    /**
     * 保存
     */
    saveOpsComputerFile(){
      http.post("/opsComputerFile/saveOpsComputerFile", this.submitData).then(response => {
        let res = response.data;
        if (!res.success) {
          // 401是token过期跳转的
          if (res.code !== 401){
            this.$message.error (res.msg);
          }
          return
        }

        this.$message.success ("保存成功");
        this.$emit('getListPage', null)
        // 关闭弹出框,继续添加
        this.tableConfig.show = false

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 更新
     */
    updOpsComputerFile(){
      clearInvalid(this.submitData)
      http.post("/opsComputerFile/updOpsComputerFile", this.submitData).then(response => {
        let res = response.data;
        if (!res.success) {
          // 401是token过期跳转的
          if (res.code !== 401){
            this.$message.error (res.msg);
          }
          return
        }

        this.$message.success ("更新成功");
        this.$emit('getListPage', null)
        // 关闭弹出框
        this.tableConfig.show = false

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
  }
}
</script>

<style scoped>

</style>


